using System.Windows.Forms;
using System.Text.RegularExpressions;
using WindowsLive.Writer.Api;
using ScottIsAFool;

namespace BoldHyperlinksPlugin
{
    [WriterPlugin("f8414f83-39c7-4f9d-b7f3-17e8fa97d786", "Bold Hyperlinks Plugin",
        PublisherUrl = "http://scottisafooldev.spaces.live.com",
        Description = "Automatically make hyperlinks bold.",
        ImagePath = "writer.png",
        HasEditableOptions = true)]

    public class Plugin : PublishNotificationHook
    {
        Settings m_default;
        
        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);
            m_default = new Settings(pluginOptions);
        }

        public override bool OnPrePublish(IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish)
        {
            string source = publishingContext.PostInfo.Contents;
            if (source.Contains("</a>"))
            {
                if (m_default.Automatic)
                {
                    return WriterUtils.ReplaceText(publishingContext.PostInfo, MakeBold(source), PostItems.Contents);
                }
                else
                {
                    DialogResult result = MessageBox.Show("Would you like to make the hyperlinks bold in your post?\n(Default can be set in Tools | Options | Plugins)", "Make Bold?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                    if (result == DialogResult.Yes)
                    {
                        return WriterUtils.ReplaceText(publishingContext.PostInfo, MakeBold(source), PostItems.Contents);
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            else
            {
                return true;
            }
        }

        public override void EditOptions(IWin32Window dialogOwner)
        {
            frmOptions op = new frmOptions(m_default);
            op.ShowDialog();
        }
        private string MakeBold(string source)
        {
            source = source.Replace("<a", "<b><a");
            source = source.Replace("</a>", "</a></b>");

            return source;
        }
    }
}
